package com.bw.controller;

import com.bw.entity.*;
import com.bw.mapper.StudentMapper;
import com.bw.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.ExcelUtils;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Controller
public class ListController {

    @Autowired
    StudentMapper studentMapper;


    @Autowired
    StudentService studentService;

    @RequestMapping("/list.do")
    public String list(StudentQuery query, Integer pageNum, HttpServletRequest request, Model model) {
        //获取当前登录用户
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);

        model.addAttribute("phone", userFromRequest.getPhone());
        model.addAttribute("nickName", userFromRequest.getNickName());
        model.addAttribute("avatarUrl", userFromRequest.getAvatarUrl());

        //分页查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);

       List<StudentRoom> list =  studentMapper.selectList(query);
        PageInfo<StudentRoom> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }


    @RequestMapping("/studentExcel.do")
    public void excel(StudentQuery query, HttpServletResponse response){
        List<StudentRoom> list =  studentMapper.selectList(query);
        ExcelUtils.exportExcel(list,"学生宿舍导出","sheet1",response);
    }
    @RequestMapping("/addPage.do")
    public String addPage(Model model){
      // 下拉框
       List<Dict> apartmentList =  studentMapper.listApartment();
        model.addAttribute("apartmentList",apartmentList);
       List<Dict> roomList =  studentMapper.listRoom();
        model.addAttribute("roomList",roomList);
        List<Dict> studentList = studentMapper.listStudent();
        model.addAttribute("studentList", studentList);

        return "add";

    }
    @RequestMapping("/add.do")
    public String add(@Valid StuRoomAdd stuRoomAdd, BindingResult result,Model model){
        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            // 下拉框
            List<Dict> apartmentList =  studentMapper.listApartment();
            model.addAttribute("apartmentList",apartmentList);
            List<Dict> roomList =  studentMapper.listRoom();
            model.addAttribute("roomList",roomList);
            List<Dict> studentList = studentMapper.listStudent();
            model.addAttribute("studentList", studentList);
            return "add";
        }

        studentService.add(stuRoomAdd);

        // 发送短信
        ZzyUtils.sendSms("18451403611","xxx1234321",5);
        return "redirect:list.do";

    }



}
